package de.tu_darmstadt.gdi.battleship.gui;

import java.awt.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Locale;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import translator.TranslatableGUIElement;
import translator.Translator;

import de.tu_darmstadt.gdi1.battleship.exceptions.InternalFailureException;
import de.tu_darmstadt.gdi1.battleship.exceptions.ParameterOutOfRangeException;
//import de.tu_darmstadt.gdi1.battleship.ui.*;

public class BattleWindow extends GameWindow{

	private String[] modi = {"normal", "easy"};
	private String path = "src/de/tu_darmstadt/gdi/resources/levels/";
	private JCheckBox cbmiTasten;
	private int coordx, coordy;
	private Locale locTarg;
	
	public BattleWindow(String windowTitle) {
		super(windowTitle);
		setResizable(true);
		setSize(550, 300);
		setLocation(450, 250);
		
		locTarg = Locale.GERMAN;
		final Translator trans = new Translator("msg", locTarg);
		TranslatableGUIElement transEle = trans.getGenerator();
		
		
		JMenuBar bar = new JMenuBar();
		JMenu datei = transEle.generateJMenu("Datei");
		JMenu hilfe = transEle.generateJMenu("Hilfe");
		JMenu lang = transEle.generateJMenu("lang");
		cbmiTasten = transEle.generateJCheckBox("Taste");
		
		
		JMenuItem newGame = transEle.generateJMenuItem("neu");
		datei.add(newGame);
		newGame.addActionListener(new AbstractAction(){
			public void actionPerformed( ActionEvent e ) {
		        try {
					notifyLevelLoaded(21,10);
					gamePanel.redraw();
				} catch (ParameterOutOfRangeException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (InternalFailureException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		      }
		});
		datei.addSeparator();
		JMenuItem save = transEle.generateJMenuItem("save");
		datei.add(save);
		save.addActionListener(new AbstractAction(){
			public void actionPerformed( ActionEvent e ) {
				JFileChooser fileChooser = new JFileChooser(path + "savedlevels/");
		        if ( fileChooser.showSaveDialog( null ) == JFileChooser.APPROVE_OPTION )
		        {
		        	File fi = new File(fileChooser.getCurrentDirectory().getPath() + "\\" +
		        			fileChooser.getName(fileChooser.getSelectedFile()) + ".lvl");
		        	
		        	try {
						FileWriter fw = new FileWriter(fi);
						BufferedWriter bfw = new BufferedWriter(fw);
						bfw.write("adfafdsaf");
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
		        	
					
		        }

				
		      }
		});
		JMenuItem load = transEle.generateJMenuItem("load");
		datei.add(load);
		load.addActionListener(new AbstractAction(){
			public void actionPerformed( ActionEvent e ) {
				JFileChooser fileChooser = new JFileChooser(path);
		        if ( fileChooser.showOpenDialog( null ) == JFileChooser.APPROVE_OPTION )
		        {
		        	String path = "src/de/tu_darmstadt/gdi/resources/";
		        	String temp = fileChooser.getCurrentDirectory().getPath();
		        	int tempNumb = 0;
		        	for(int i = temp.length() - 1;temp.charAt(i) != File.separatorChar;i--)
		        	{
		        		tempNumb++;
		        	}
		        	
		        	for(int i = tempNumb - 1;i >= 0;i--)
		        	{
		        		path += temp.charAt(temp.length() - 1 - i);
		        	}
		        		
		        	path += "/" + fileChooser.getName(fileChooser.getSelectedFile());
		        	
		        	//TODO path an logik geben und das spiel starten
		        	keyNewGamePressed();
		        }
		    }
		});
		JMenuItem opti = transEle.generateJMenuItem("option");
		datei.add(opti);
		opti.addActionListener(new AbstractAction(){
			public void actionPerformed( ActionEvent e ) {
				OptionMenu opt = new OptionMenu(getThis(), "Optionen", locTarg);
				opt.setVisible(true);
		      }
		});
		datei.addSeparator();
		JMenuItem end  = transEle.generateJMenuItem("end");
		datei.add(end);
		end.addActionListener(new AbstractAction() {
		      public void actionPerformed( ActionEvent e ) {
		        System.exit( 0 );
		      }
		    } );

		JMenuItem hand = transEle.generateJMenuItem("hand");
		hilfe.add(hand);
		hand.addActionListener(new AbstractAction() {
		      public void actionPerformed( ActionEvent e ) {
		        BattleManual bm = new BattleManual("Manual", locTarg);
		        bm.setVisible(true);
		      }
		    } );
		JMenuItem about = transEle.generateJMenuItem("about");
		hilfe.add(about);
		about.addActionListener(new AbstractAction() {
		      public void actionPerformed( ActionEvent e ) {
		        AboutWindow aw = new AboutWindow("About", locTarg);
		        aw.setVisible(true);
		      }
		    } );
		
		JMenuItem ger = new JMenuItem("German");
		ger.addActionListener(new AbstractAction(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				locTarg = new Locale("");
				trans.setTranslatorLocale(locTarg);
			}
		});
		JMenuItem eng = new JMenuItem("English");
		ger.addActionListener(new AbstractAction(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				locTarg = new Locale("");
				trans.setTranslatorLocale(locTarg);
			}
		});
		JMenuItem nor = new JMenuItem("Norsk");
		ger.addActionListener(new AbstractAction(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				locTarg = new Locale("");
				trans.setTranslatorLocale(locTarg);
			}
		});
		
		
		
		transEle.translateGUIElements();
		
		bar.add(datei);
		bar.add(cbmiTasten);
		bar.add(lang);
		bar.add(hilfe);
		setJMenuBar(bar);
		
	}

	@Override
	protected GamePanel createGamePanel() {
		gamePanel = new BattlePanel(this);
		add(gamePanel);
		return gamePanel;
	}
	
	public BattlePanel getBattlePanel()
	{
		return (BattlePanel) gamePanel;
	}
	
	private BattleWindow getThis()
	{
		return this;
	}
	
	public String[] getMode()
	{
		return modi;
	}
	
	protected void setMode(String[] newMode)
	{
		modi = newMode;
	}
	
	@Override
	protected void keyNewGamePressed()
	{
		//TODO start a new Game
		try {
			notifyLevelLoaded(21,10);
			gamePanel.redraw();
		} catch (ParameterOutOfRangeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InternalFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	protected void keyQuitPressed()
	{
		System.exit(0);
	}
	
	@Override
	protected void keyRedoPressed()
	{
		if(cbmiTasten.isSelected())
			gamePanel.entityClicked(coordx, coordy);
	}
	
	@Override
	protected void keyUpPressed()
	{
		if(cbmiTasten.isSelected())
		{
			if(coordy > 0)
			{
				coordy--;
				getBattlePanel().drawKeyBorder(coordx, coordy);
			}
		}
	}
	
	@Override
	protected void keyDownPressed()
	{
		if(cbmiTasten.isSelected())
		{
			if(coordy < 9)
			{
				coordy++;
				getBattlePanel().drawKeyBorder(coordx, coordy);
			}
		}
	}
	
	@Override
	protected void keyLeftPressed()
	{
		if(cbmiTasten.isSelected())
		{
			if(coordx > 0)
			{
				coordx--;
				getBattlePanel().drawKeyBorder(coordx, coordy);
			}
		}
	}
	
	@Override
	protected void keyRightPressed()
	{
		if(cbmiTasten.isSelected())
		{
			if(coordx < 9)
			{
				coordx++;
				getBattlePanel().drawKeyBorder(coordx, coordy);
			}
		}
	}
	
	protected int getXCoordinate()
	{
		return coordx;
	}
	
	protected int getYCoordinate()
	{
		return coordy;
	}
	
	
}
